﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Brain;
namespace AvaProvi
{
    public partial class ctlAnalisisFisico : AvaProvi.ControlBase
    {
        VSimbolo vs;
        Validadora val;

        public ctlAnalisisFisico()
            : base()
        {
            InitializeComponent();
            ActualizaPanel();
        }

        public override void ActualizaDatos()
        {
            clsAvaluo_CRN.AvaluoActual.Guardar();
            ActualizaPanel();
        }

        public override void ActualizaPanel()
        {
            this.bsTerreno.DataSource = clsAvaluo_CRN.AvaluoActual.Inmueble.Terreno;
            this.bsConstruccion.DataSource = clsAvaluo_CRN.AvaluoActual.Inmueble.Terreno.Construccion;
            this.bsConstruccion.ResetBindings(false);
            this.bsTerreno.ResetBindings(false);
        }

        private void gvSupAccesoria_Click(object sender, EventArgs e)
        {
            if (clsAvaluo_CRN.AvaluoActual.Inmueble.Terreno.SuperficiesAccesorias.Count == 0)
            {
                AgregaSuperficieAccesoria();
            }
        }

        private void gvSupAccesoria_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                AgregaSuperficieAccesoria();
            }
        }

        private void gvSupAccesoria_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
        {
            clsSuperficieAccesoria_CRN objSuperficieEliminada = (clsSuperficieAccesoria_CRN)(e.Row.DataBoundItem);
            objSuperficieEliminada.EsEliminado = true;
            objSuperficieEliminada.Guardar();
        }

        private void AgregaSuperficieAccesoria()
        {
            clsSuperficieAccesoria_CRN objSuperficie = new clsSuperficieAccesoria_CRN(clsAvaluo_CRN.AvaluoActual.Inmueble.Terreno);
            clsAvaluo_CRN.AvaluoActual.Inmueble.Terreno.SuperficiesAccesorias.Add(objSuperficie);
            this.bsTerreno.ResetBindings(false);

        }

        private void AgregaAreaComun()
        {
            clsAreaComun_CRN objArea = new clsAreaComun_CRN(clsAvaluo_CRN.AvaluoActual.Inmueble.Terreno);
            clsAvaluo_CRN.AvaluoActual.Inmueble.Terreno.AreasComunes.Add(objArea);
            this.bsTerreno.ResetBindings(false);
        }

        private void gvAreasComunes_Click(object sender, EventArgs e)
        {
            if (clsAvaluo_CRN.AvaluoActual.Inmueble.Terreno.AreasComunes.Count == 0)
            {
                AgregaAreaComun();
            }
        }

        private void gvAreasComunes_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                AgregaAreaComun();
            }
        }

        private void gvAreasComunes_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
        {
            clsAreaComun_CRN objAreaEliminada = (clsAreaComun_CRN)(e.Row.DataBoundItem);
            objAreaEliminada.EsEliminado = true;
            objAreaEliminada.Guardar();
        }

        private void gvFactoresConstruc_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            this.bsTerreno.ResetBindings(false);
            this.bsConstruccion.ResetBindings(false);
        }

        private void resultadoDeAnalisisTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            vs = new VSimbolo();
            e.Handled = vs.esDecimal(e.KeyChar, (resultadoDeAnalisisTextBox.Text).Trim());
        }

        private void resultadoPorFactorAjusteTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            vs = new VSimbolo();
            e.Handled = vs.esDecimal(e.KeyChar, (resultadoPorFactorAjusteTextBox.Text).Trim());
        }

        private void superficieTotalTerrenoTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            vs = new VSimbolo();
            e.Handled = vs.esDecimal(e.KeyChar, (superficieTotalTerrenoTextBox.Text).Trim());
        }

        private void valorParcialTerrenoTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            vs = new VSimbolo();
            e.Handled = vs.esDecimal(e.KeyChar, (valorParcialTerrenoTextBox.Text).Trim());
        }

        private void gvFactores_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            TextBox txt = (TextBox)e.Control;
            txt.KeyPress += validar_Keypress_gvFact; 
        }
        private void validar_Keypress_gvFact(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            int c = gvFactores.CurrentCell.ColumnIndex;
            if (c == 1 || c == 2 || c == 3 || c == 4 || c == 5 || c == 6 || c == 7 || c == 8 )
            {
                char caracter = e.KeyChar;
                // referencia a la celda   
                TextBox txt = (TextBox)sender;
                vs = new VSimbolo();
                e.Handled = vs.esDecimal(caracter, (txt.Text).Trim());
            }
        }
        private void gvFactores_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            string dato = e.FormattedValue.ToString();
            val = new Validadora();
            int gv = gvFactores.Columns[e.ColumnIndex].Index;
            if (gv == 1 || gv == 2 || gv == 3 || gv == 4 || gv == 5 || gv == 6 || gv == 7 || gv == 8 )
            {
                if (!val.esReal(dato))
                {
                    e.Cancel = true;
                }
            }

        }

        private void gvValorTerreno_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            TextBox txt = (TextBox)e.Control;
            txt.KeyPress += validar_Keypress_gvVTerr;
        }
        private void validar_Keypress_gvVTerr(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            int c = gvValorTerreno.CurrentCell.ColumnIndex;
            if (c == 1 || c == 2 || c == 3 || c == 4 || c == 5 || c == 6 || c == 7 )
            {
                char caracter = e.KeyChar;
                // referencia a la celda   
                TextBox txt = (TextBox)sender;
                vs = new VSimbolo();
                e.Handled = vs.esDecimal(caracter, (txt.Text).Trim());
            }
        }
        private void gvValorTerreno_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            string dato = e.FormattedValue.ToString();
            val = new Validadora();
            int gv = gvValorTerreno.Columns[e.ColumnIndex].Index;
            if (gv == 1 || gv == 2 || gv == 3 || gv == 4 || gv == 5 || gv == 6 || gv == 7)
            {
                if (!val.esReal(dato))
                {
                    e.Cancel = true;
                }
            }
        }

        private void gvFactoresConstruc_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            TextBox txt = (TextBox)e.Control;
            txt.KeyPress += validar_Keypress_gvFacCons;
        }
        private void validar_Keypress_gvFacCons(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            int c = gvFactoresConstruc.CurrentCell.ColumnIndex;
            if (c == 1 || c == 2 || c == 3 || c == 4 || c == 5 || c == 6)
            {
                char caracter = e.KeyChar;
                // referencia a la celda   
                TextBox txt = (TextBox)sender;
                vs = new VSimbolo();
                e.Handled = vs.esDecimal(caracter, (txt.Text).Trim());
            }
        }
        private void gvFactoresConstruc_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            string dato = e.FormattedValue.ToString();
            val = new Validadora();
            int gv = gvFactoresConstruc.Columns[e.ColumnIndex].Index;
            if (gv == 1 || gv == 2 || gv == 3 || gv == 4 || gv == 5 || gv == 6)
            {
                if (!val.esReal(dato))
                {
                    e.Cancel = true;
                }
            }
        }

        private void gvValorDeConstrucc_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            TextBox txt = (TextBox)e.Control;
            txt.KeyPress += validar_Keypress_gvValDC;
        }
        private void validar_Keypress_gvValDC(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            int c = gvValorDeConstrucc.CurrentCell.ColumnIndex;
            if (c == 2 || c == 3 || c == 4 || c == 5 || c == 6 || c == 7)
            {
                char caracter = e.KeyChar;
                // referencia a la celda   
                TextBox txt = (TextBox)sender;
                vs = new VSimbolo();
                e.Handled = vs.esDecimal(caracter, (txt.Text).Trim());
            }
        }
        private void gvValorDeConstrucc_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            string dato = e.FormattedValue.ToString();
            val = new Validadora();
            int gv = gvValorDeConstrucc.Columns[e.ColumnIndex].Index;
            if (gv == 2 || gv == 3 || gv == 4 || gv == 5 || gv == 6 || gv == 7)
            {
                if (!val.esReal(dato))
                {
                    e.Cancel = true;
                }
            }
        }

        private void superficieDeConstruccionesTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            vs = new VSimbolo();
            e.Handled = vs.esDecimal(e.KeyChar, (superficieDeConstruccionesTextBox.Text).Trim());
        }

        private void valorTotalAsegurableDeConstrucTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            vs = new VSimbolo();
            e.Handled = vs.esDecimal(e.KeyChar, (valorTotalAsegurableDeConstrucTextBox.Text).Trim());
        }

        private void valorParcialDeConstruccionesTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            vs = new VSimbolo();
            e.Handled = vs.esDecimal(e.KeyChar, (valorParcialDeConstruccionesTextBox.Text).Trim());
        }

        private void superficieAccesoriaTotalTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            vs = new VSimbolo();
            e.Handled = vs.esDecimal(e.KeyChar, (superficieAccesoriaTotalTextBox.Text).Trim());
        }

        private void valorTotalAsegurableDeSupAccesoriaTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            vs = new VSimbolo();
            e.Handled = vs.esDecimal(e.KeyChar, (valorTotalAsegurableDeSupAccesoriaTextBox.Text).Trim());
        }

        private void valorParcialDeSupAccesoriaTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            vs = new VSimbolo();
            e.Handled = vs.esDecimal(e.KeyChar, (valorParcialDeSupAccesoriaTextBox.Text).Trim());
        }

        private void superficieVendibleTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            vs = new VSimbolo();
            e.Handled = vs.esDecimal(e.KeyChar, (superficieVendibleTextBox.Text).Trim());
        }

        private void valorTotalDeConstruccionTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            vs = new VSimbolo();
            e.Handled = vs.esDecimal(e.KeyChar, (valorTotalDeConstruccionTextBox.Text).Trim());
        }

        private void gvSupAccesoria_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            TextBox txt = (TextBox)e.Control;
            txt.KeyPress += validar_Keypress_gvSA;
        }
        private void validar_Keypress_gvSA(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            int c = gvSupAccesoria.CurrentCell.ColumnIndex;
            if (c == 0)
            {
                char caracter = e.KeyChar;
                // referencia a la celda   
                TextBox txt = (TextBox)sender;
                vs = new VSimbolo();
                e.Handled = vs.esEntero(caracter, (txt.Text).Trim());
            }
            if (c == 2 || c == 3 || c == 4 || c == 6 || c == 7)
            {
                char caracter = e.KeyChar;
                // referencia a la celda   
                TextBox txt = (TextBox)sender;
                vs = new VSimbolo();
                e.Handled = vs.esDecimal(caracter, (txt.Text).Trim());
            }
            
        }
        private void gvSupAccesoria_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            string dato = e.FormattedValue.ToString();
            val = new Validadora();
            int gv = gvSupAccesoria.Columns[e.ColumnIndex].Index;
            if (gv == 0 )
            {
                if (!val.esEntero(dato))
                {
                    e.Cancel = true;
                }
            }
            if (gv == 2 || gv == 3 || gv == 4 ||  gv == 6 || gv == 7)
            {
                if (!val.esReal(dato))
                {
                    e.Cancel = true;
                }
            }

        }

        private void valorTotalAsegurableDeAreasCTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            vs = new VSimbolo();
            e.Handled = vs.esDecimal(e.KeyChar, (valorTotalAsegurableDeAreasCTextBox.Text).Trim());
        }

        private void valorParcialDeAreasComunesTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            vs = new VSimbolo();
            e.Handled = vs.esDecimal(e.KeyChar, (valorParcialDeAreasComunesTextBox.Text).Trim());
        }

        private void gvAreasComunes_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            TextBox txt = (TextBox)e.Control;
            txt.KeyPress += validar_Keypress_gvACom;
        }
        private void validar_Keypress_gvACom(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            int c = gvAreasComunes.CurrentCell.ColumnIndex;
            if (c == 1 || c == 3 || c == 4 || c == 6 || c == 7 || c == 8)
            {
                char caracter = e.KeyChar;
                // referencia a la celda   
                TextBox txt = (TextBox)sender;
                vs = new VSimbolo();
                e.Handled = vs.esDecimal(caracter, (txt.Text).Trim());
            }

        }
        private void gvAreasComunes_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            string dato = e.FormattedValue.ToString();
            val = new Validadora();
            int gv = gvAreasComunes.Columns[e.ColumnIndex].Index;
            if (gv == 1 || gv == 3 || gv == 4 || gv == 6 || gv == 7 || gv == 8)
            {
                if (!val.esReal(dato))
                {
                    e.Cancel = true;
                }
            }
        }
    }
}
